package com.pandacc.common;

//在网上找了好半天，没有发现好的中文句子断句方法，无奈自己查阅资料结合部分网上代码改写了一个，如下：
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;


/**
* @author Cavatina
* @version 2013-3-25 下午04:04:27
* 
*/
public class SentenceProcessor {

	/**将文本内容切分为句子
	 * @param content
	 * @return 切分好的句子列表List<NewsSent>
	 */
	public List<String> segSentence(String content) {
		List<String> list = new ArrayList<String>();
		String temp = content;
		temp = StringUtils.deleteWhitespace(temp);
		temp = StringUtils.trim(temp).replaceAll("(@)?(href=\")?(http://)?[A-Za-z]+(\\.\\w+)+(/[&\\n=?\\+\\%/\\.\\w]+)?","");
	    temp =StringUtils.trim(temp).replaceAll("[`^&*()+=|{}\\[\\]<>/￥%（）——|__:【】‘”“’]","");
		temp = temp.replaceAll("(\\d{0,4})-?(\\d{2})-(\\d{1,2}) ?\\d{2}:\\d{2}:?\\d{0,2}","")
		        .replaceAll("★|●|\\*|\\/|'+", "")
		        .replaceAll("~", "。")
		        .replaceAll("～", "。")
				.replaceAll("！", "。")
				.replaceAll("!", "。")
				.replaceAll("？", "。")
				.replaceAll("﹖", "。")
				.replaceAll(";", "。")
				.replaceAll("；", "。")
				.replaceAll("。+", "。")
				.replaceAll("\\.\\.", "。")
				.replaceAll("……", "。");

		// 根据中文标点符号切分
		BreakIterator boundary = BreakIterator.getSentenceInstance(Locale.CHINESE);
      //设置句子在文中的位置索引
		int index = 1;
		// 设置要处理的文本
		boundary.setText(temp);
		int start = boundary.first(); // 设置开始位置
		for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
			// 输出子串
				
			String str = temp.substring(start, end);
			if(isSentence(str) == true)
			 {
				/*SentenceBean newsSen = new SentenceBean();
				newsSen.setSentId(index);
				newsSen.setSentence(str);*/
				list.add(str);
				index++;
			 }
		}
		return list;
	}
	
	/**
	 * 判断切分字符串是否为句子
	 * 
	 * @param str
	 * @return boolean
	 */
	public boolean isSentence(String str) {
		boolean isSen = false;
      if(isChinese(str.charAt(0)) == true){isSen = true;}//第一个字符为中文则判定该句为中文
      else{
         if(isChinese(str) == true){isSen = true;}//句子中含中文则判定该句为中文
      }
		return isSen;
	}

	/**判断输入字符是否为中文
	 * @param c 
	 * @return boolean
	 */
	private final boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
			  //|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
			  //|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				) {
			return true;
		}
		return false;
	}

	/**判断输入字符串是否为中文
	 * @param strName
	 * @return boolean
	 */
	public boolean isChinese(String strName) {
		char[] ch = strName.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if (isChinese(c)) {
				return true;
			}
		}
		return false;
	}

	  /**利用通用编码区间来判断是否为中文,编码区间为[0x4e00-0x9fbb][\u4E00-\u9FA5]"
	 * @param chineseStr
	 * @return boolean
	 */
	public boolean isChineseCharacter(String chineseStr) {  
	        char[] charArray = chineseStr.toCharArray();  
	        for (int i = 0; i < charArray.length; i++) {  
	            if ((charArray[i] >= 0x4e00) && (charArray[i] <= 0x9fbb)) {  
	                return true;  
	            }
	        }  
	        return false;  
	    }
	
	//
	@SuppressWarnings("resource")
	public static void main(String args[]) throws IOException {
		String baseDir = System.getProperty("user.dir");
		String filePath = baseDir + File.separator + "src/main/resources/射雕英雄传.txt";
		long start = System.currentTimeMillis();
		SentenceProcessor seg = new SentenceProcessor();
//		System.out.println(seg.isChineseCharacter("。"));
		Pattern p = Pattern.compile("\r|\n");
		String lineSeparator = System.getProperty("line.separator");
		StringBuilder sb = new StringBuilder();
		String temp = null;
		byte[] tempByteArr = null;
		BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)),"GBK"));
		while ((temp = reader.readLine()) != null) {
			boolean flag = temp.contains("\r\n") || temp.contains("/n");
			System.out.println(flag+" -> " + temp);
			if(flag){
				sb.append(temp);
				System.out.println("<p>"+sb+"</p>");
				sb = new StringBuilder("");
			}
		}
		/*List<SentenceBean> list = seg.segSentence(sb.toString());
		for(SentenceBean sent : list)
		{
			System.out.println("<p>"+sent.getSentence()+"</p>");
		}*/
		System.out.println(System.currentTimeMillis() - start);
	}
}
